<?php
session_start();
error_reporting('E_NOTICE');
$self = $_SERVER['PHP_SELF'];

include 'db-control/inc/functions.php';
include 'admin/functions.php';
if(!isStudent()){
   die("Please log in <a href='index.php'>HERE</a> first.");
}

?>
<!DOCTYPE html>
<html>
   <head>
      <title>Internships2Career</title>
      <link rel="stylesheet" type="text/css" href="css/style.css" />
      <script type="text/javascript">
         function updateSearchField(){
            var val = search_form.elements["searchFor"].value;
            search_form.search.placeholder = val;
         }
      </script>
   </head>
   <body>
      <form action="<?php echo $self; ?>" method="get">
         <table width="100%">
            <div align="left">
               <tr><td rowspan="6"><img alt="" src="img/unhmlogo.png" width="200px" height="120px" /></td></tr>
               <tr><td align="right" valign="top"><a href="logout.php" title="Logout of the system.">Logout</a> | <a href="help.php" title="View the help documentation.">?</a></td></tr>
               <tr><td width="100%" align="left"><h3 style="font-size:2em;">Internship 2 Career</h3><br /></td></tr>
               <tr><td align="left"><p>You can filter your searches by category, city, or a custom search:</p></td></tr>
               <tr>
                  <td align="left">
                     <?php
                     categoryDropDown();
                     cityDropDown();
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="left">
                     <form action="<?php echo $self; ?>" method="get" id="search_form">
                        <?php
                        searchBox();
                        searchFor();
                        ?>
                     </form>
                  </td>
               </tr>
               <tr>
                  <td></td><td align="left"><?php ?></td>
               </tr>
            </div>
         </table>
         <br /><br />
      </form>
      <?php include 'view-control/internship_list.php'; ?>
                     </body>
                  </html>


<?php

                        function searchFor() {
                           if (isset($_REQUEST['searchFor'])) {
                              $val = $_REQUEST['searchFor'];
                              $_SESSION['searchFor'] = $val;
                           } else {
                              $val = $_SESSION['searchFor'];
                           }
                           switch ($val) {
                              case 'Positions':
                                 $pos = 'SELECTED';
                                 break;
                              case 'Qualifications':
                                 $qual = 'SELECTED';
                                 break;
                              case 'Requirements':
                                 $req = 'SELECTED';
                                 break;
                              case 'Organizations':
                                 $org = 'SELECTED';
                                 break;
                           }
                           echo "<select onchange='updateSearchField();' style='width:120px;' name='searchFor'>";
                           echo "<option $pos value='Positions'>Positions</option>";
                           echo "<option $qual value='Qualifications'>Qualifications</option>";
                           echo "<option $req value='Responsibilities'>Responsibilities</option>";
                           echo "<option $org value='Organizations'>Company</option>";
                           echo "</select>";
                        }

                        function searchBox() {
                           if (isset($_REQUEST['search'])) {
                              $val = $_REQUEST['search'];
                              $_SESSION['search'] = $val;
                           } else {
                              $val = $_SESSION['search'];
                           }
                           $ph = $_SESSION['searchFor'];
                           echo "Search: <input type='search' title='Search for specific positions' name='search' onblur='this.form.submit();' placeholder='$ph' value='$val'/>";
                        }

                        /**
                         * @desc Displays a select box for categories
                         * @param string $name
                         */
                        function categoryDropDown() {
                           include 'db-control/dbCategory.php';
                           $result = dbCategory::getAllCategories();
                           if (isset($_REQUEST['categoryChange'])) {
                              $catName = $_REQUEST['categoryChange'];
                              $_SESSION['category'] = $catName;
                           } else {
                              $catName = $_SESSION['category'];
                           }
                           $numRows = mysql_num_rows($result);

                           echo "<form action='$self' method='post'>";
                           echo "Category: <select title='Select a category to only show internships in that category.' onchange='this.form.submit()' name='categoryChange'>";
                           if ($catName == '') {
                              echo "<option SELECTED value=''>Select a category...</option>";
                           } else {
                              echo "<option value=''>Show All</option>";
                           }
                           for ($i = 0; $i < $numRows; $i++) {
                              $row = mysql_fetch_assoc($result);
                              $name = $row['categoryName'];
                              if ($row['categoryName'] == $catName) {
                                 echo "<option SELECTED value='$name'>$name</option>";
                              } else {
                                 echo "<option value='$name'>$name</option>";
                              }
                           }
                           echo "</select>";
                           echo "</form>";
                        }

                        function cityDropDown() {
                           include 'db-control/dbOrganization.php';
                           $result = dbOrganization::getCities();
                           if (isset($_REQUEST['cityChange'])) {
                              $city = $_REQUEST['cityChange'];
                              $_SESSION['city'] = $city;
                           } else {
                              $city = $_SESSION['city'];
                           }

                           $numRows = mysql_num_rows($result);
                           echo "<form action='$self' method='get'>";
                           echo "City: <select title='Select a city to filter internships by that location.' onchange='this.form.submit()' name='cityChange'>";
                           if ($city == null) {
                              echo "<option value=''>Select a city...</option>";
                           } else {
                              echo "<option value=''>Show All</option>";
                           }
                           for ($i = 0; $i < $numRows; $i++) {
                              $row = mysql_fetch_assoc($result);
                              if (strcmp($row['city'], $city) == 0) {
                                 echo "<option SELECTED value='" . $row['city'] . "'>" . $row['city'] . "</option>";
                              } else {
                                 echo "<option value='" . $row['city'] . "'>" . $row['city'] . "</option>";
                              }
                           }
                           echo "</select>";
                           echo "</form>";
                        }
?>
